//
//  MailViewController.m
//  SP2P_7
//
//  Created by Jerry on 14-6-18.
//  Copyright (c) 2014年 EIMS. All rights reserved.
//
//账户中心---》信箱
#import "MailViewController.h"
#import "MailTableViewCell.h"
#import "SystemMessageViewController.h"
#import "InBoxViewController.h"
#import "OutBoxViewController.h"
#import "WriteMailViewController.h"
#import "BorrowimgQuestionsViewController.h"
#import "TabViewController.h"

@interface MailViewController ()<UITableViewDataSource,UITableViewDelegate,HTTPClientDelegate>
{

    NSMutableArray *titileArr;
    SystemMessageViewController *systemMessageView;
    InBoxViewController *inBoxView;
    OutBoxViewController *outBoxView;
    WriteMailViewController *writeMailView;
    BorrowimgQuestionsViewController *bQuestion;

    NSInteger _unreadInboxMsgCount;
    NSInteger _unreadSystemMsgCount;
}
@property (nonatomic,strong ) UITableView   *TableView;
@property (nonatomic ,strong) NetWorkClient *requestClient;
@property (nonatomic, assign) NSInteger     requestType;

@end


@implementation MailViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self requestData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (_requestClient != nil) {
        [_requestClient cancel];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 初始化数据
    [self initData];
    
    // 初始化视图
    [self initView];
    
}

#pragma mark - Data
/**
 * 初始化数据
 */
- (void)initData
{
    titileArr = [NSMutableArray arrayWithObjects:@"系统消息",   @"收件箱",@"发件箱",nil]; //@"借款提问"
    
    [self requestData];
}

- (void)requestData {
    if (AppDelegateInstance.userInfo == nil) {
        [ReLogin goLogin:self];
        return;
    }else {
        _requestType = 1;
        
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        
        [parameters setObject:@"522" forKey:@"OPT"];
        [parameters setObject:@"" forKey:@"body"];
        [parameters setObject:AppDelegateInstance.userInfo.userId forKey:@"id"];
        
        if (_requestClient == nil) {
            _requestClient = [[NetWorkClient alloc] init];
            _requestClient.delegate = self;
        }
        [_requestClient requestGet:@"app/services" withParameters:parameters];
    }
}

- (void)httpResponseSuccess:(NetWorkClient *)client dataTask:(NSURLSessionDataTask *)task didSuccessWithObject:(id)obj {
    NSDictionary *dics = (NSDictionary *)obj;
    DLOG(@"dataDics is %@",dics);
    if ([[NSString stringWithFormat:@"%@",[dics objectForKey:@"error"]] isEqualToString:@"-1"]) {
        
        _unreadInboxMsgCount = [[dics objectForKey:@"unreadInboxMsgCount"] integerValue];
        _unreadSystemMsgCount = [[dics objectForKey:@"unreadSystemMsgCount"] integerValue];
        [_TableView reloadData];
        
    }else {
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", [dics objectForKey:@"msg"]]];
    }
}

#pragma mark - View
/**
 初始化数据
 */
- (void)initView
{
    
    [self initNavigationBar];
    self.view.backgroundColor = [UIColor whiteColor];
    //列表视图
    _TableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    _TableView.delegate = self;
    _TableView.dataSource = self;
    [self.view addSubview:_TableView];

    //子视图初始化
    systemMessageView = [[SystemMessageViewController alloc] init];
    inBoxView = [[InBoxViewController alloc] init];
    outBoxView = [[OutBoxViewController alloc] init];
    writeMailView = [[WriteMailViewController alloc] init];
    bQuestion = [[BorrowimgQuestionsViewController alloc] init];
}


/**
 * 初始化导航条
 */
- (void)initNavigationBar
{
    [self initNavBarTitle:@"站内信" andLeftItemName:@"返回" andRightItemName:@"investment_calculator_normal*image"];

    
}

#pragma mark - 表视图协议代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return titileArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cellid";
    MailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[MailTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1  reuseIdentifier:cellId];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.accessoryView = [UIImageView imageViewWithFrame:RECTMAKE(0, 0, 9, 18) imageFile:@"common_rightarrow" superview:nil];
    cell.textLabel.textColor = KBlackColor;
    cell.textLabel.font = KFontSize(15.0);
    cell.textLabel.text = [titileArr objectAtIndex:indexPath.row];
    
    cell.unreadNumLabel.hidden = YES;
    if (indexPath.row == 0) {
        if (_unreadSystemMsgCount > 0) {
            cell.unreadNumLabel.hidden = NO;
            NSString *unreadStr = [NSString stringWithFormat:@"%ld",_unreadSystemMsgCount];
            if (_unreadSystemMsgCount > 99) {
                unreadStr = @"99+";
            }
            CGFloat width = [MyTools getSizeOfString:unreadStr maxWidth:100 maxHeight:20 withFontSize:15].width + 15;
            cell.unreadNumLabel.frame = RECTMAKE(MSWIDTH - 30 - width, KAdaptNum(15), width, KAdaptNum(20));
            cell.unreadNumLabel.text = unreadStr;
            
        }
    }else if (indexPath.row == 1) {
        if (_unreadInboxMsgCount > 0) {
            cell.unreadNumLabel.hidden = NO;
            NSString *unreadStr = [NSString stringWithFormat:@"%ld",_unreadInboxMsgCount];
            if (_unreadInboxMsgCount > 99) {
                unreadStr = @"99+";
            }
            CGFloat width = [MyTools getSizeOfString:unreadStr maxWidth:100 maxHeight:20 withFontSize:15].width + 15;
            cell.unreadNumLabel.frame = RECTMAKE(MSWIDTH - 30 - width, KAdaptNum(15), width, KAdaptNum(20));
            cell.unreadNumLabel.text = unreadStr;
            
        }
    }
  
    cell.frame=CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, KAdaptNum(50));
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:
            [self.navigationController pushViewController:systemMessageView animated:YES];
            break;
        case 1:
            [self.navigationController pushViewController:inBoxView animated:YES];
            break;
        case 2:
            [self.navigationController pushViewController:outBoxView animated:YES];
            break;
        case 3:
            [self.navigationController pushViewController:bQuestion animated:YES];
            break;
    }
}

#pragma mark - 写信触发方法
- (void)rightBarButtonClick {
    [self.navigationController pushViewController:writeMailView animated:YES];
}



@end
